package com.xuyifeng.callfilter;

import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.provider.CallLog;

/* An adapter to display customized view */
class CallLogCursorAdapter extends CursorAdapter {
	CallLogCursorAdapter(Context context, Cursor c) {
		super(context, c);
	}

	CallLogCursorAdapter(Context context, Cursor c, boolean autoRequery) {
		super(context, c, autoRequery);
	}
	
	public void bindView (View view, Context context, Cursor cursor) {
		setViewData(view, cursor);
	}
	
	public View	newView(Context context, Cursor cursor, ViewGroup parent)
	{
		LinearLayout view;
		
		view = new LinearLayout(context);
		String inflater = Context.LAYOUT_INFLATER_SERVICE;
		LayoutInflater vi = (LayoutInflater)context.getSystemService(inflater);
		vi.inflate(R.layout.call_log_item, view, true);
		setViewData(view, cursor);
		
		return view;
	}
	
	private void setViewData(View view, Cursor cursor) {
		TextView phoneView = (TextView)view.findViewById(R.id.callLogPhoneNum);
		TextView dateView = (TextView)view.findViewById(R.id.callLogDate);
		TextView callerView = (TextView)view.findViewById(R.id.callerName);
		String phoneStr = cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.NUMBER));
		Date date = new Date(cursor.getLong(cursor.getColumnIndexOrThrow(CallLog.Calls.DATE)));
		String caller = (cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME)));
		phoneView.setText(phoneStr);
		dateView.setText(date.toLocaleString());
		callerView.setText(caller);
	}
}

public class CallLogPicker extends Activity {
	
	Cursor m_callCursor;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.call_log_pick_view);
        
        ListView callListView = (ListView)this.findViewById(R.id.callLogListView);
        
        callListView.setOnItemClickListener(new OnItemClickListener() {
        	@Override
        	public void onItemClick(AdapterView<?> _av, View _v, int pos,
        			long arg3) {
        		// Move the cursor to the selected item
        		m_callCursor.moveToPosition(pos);
        		// Extract the phone number
        		String phoneNum = m_callCursor.getString(m_callCursor.getColumnIndexOrThrow(CallLog.Calls.NUMBER));
        		Intent outData = new Intent();
        		outData.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNum);
        		setResult(Activity.RESULT_OK, outData);
        		finish();		
        	}
        });
        
        String[] strFields = {
        		CallLog.Calls._ID, 
                CallLog.Calls.NUMBER, 
                CallLog.Calls.TYPE,
                CallLog.Calls.CACHED_NAME,
                CallLog.Calls.CACHED_NUMBER_TYPE,
                CallLog.Calls.DATE
                };
        String strOrder = android.provider.CallLog.Calls.DATE + " DESC"; 
         
        m_callCursor = managedQuery(
                android.provider.CallLog.Calls.CONTENT_URI,
                strFields,
                CallLog.Calls.NUMBER + " > 0",
                null,
                strOrder
                );
   
        CallLogCursorAdapter adapter = new CallLogCursorAdapter(this, m_callCursor);
        callListView.setAdapter(adapter);
    }
}
